package com.itheima.homework03;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class UploadThread implements Runnable {
    private Socket socket;

    public UploadThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            File file = new File("d:/test");
            file.mkdirs();
            //// 文件名：域名+当前时间毫秒值+6位随机数.txt
            String fileName = "com.itheima" + System.currentTimeMillis() + ".txt";
            FileOutputStream fos = new FileOutputStream(new File(file,fileName));
            InputStream is = socket.getInputStream();
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = is.read(buf)) != -1) {
                fos.write(buf,0,len);
            }
            socket.getOutputStream().write("上传成功".getBytes());
            socket.close();
            fos.close();

        }catch (Exception e) {

        }
    }
}
